// pages/clubprofile/clubprofile.js
var util = require("../../utils/util.js")
const {
  $Message
} = require('../../iview/base/index');
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabarr: ["社团详情", "近期活动", "往期活动"],
    TabCur: 0,
    scrollLeft: 0,
    events: [],
    subbutton: true,
    buttontext: "关注该社团"
  },

  subscribe: function(e) {
    const db = wx.cloud.database()
    var that = this
    var temp
    db.collection("user").where({
      _openid: app.globalData.openID
    }).get().then(function(res) {
      console.log(res.data[0])
      if (res.data[0].myclub == undefined) {
        temp = [that.data.clubId]
      } else {
        if (res.data[0].myclub.indexOf(that.data.clubId) != -1) {
          temp = res.data[0].myclub
          temp.splice(temp.indexOf(app.globalData.openID), 1)
        } else {
          temp = res.data[0].myclub
          temp.push(that.data.clubId)
        }
      }
    }).then(function(e) {
      console.log(temp)
      if (temp != undefined) {
        wx.cloud.callFunction({
          name: 'subscribeClub',
          data: {
            myclub: temp
          }
        }).then(function(cloudres) {
          console.log(cloudres)
          if (that.data.buttontext == "关注该社团") {
            $Message({
              content: '关注成功',
              type: "success"
            })
            that.setData({
              buttontext: "取消关注"
            })
          } else {
            that.setData({
              buttontext: "关注该社团"
            })
            $Message({
              content: '取消关注成功',
              type: "success"
            })
          }
        })
      }
    })
  },

  gotoEvent: function(e) {
    console.log(e)
    wx.navigateTo({
      url: '../../pages/eventdetail/eventdetail?id=' + e.currentTarget.id,
    })
  },

  tabSelect(e) {
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      clubId: options.id
    })
    wx.setNavigationBarTitle({
      title: '社团详情',
    })
    var date = new Date()
    this.setData({
      time: date
    })
    const db = wx.cloud.database()
    const eventlist = db.collection("event")
    const clublist = db.collection("club");
    var that = this
    db.collection("user").where({
      _openid: options.userOpenId
    }).get().then(function(userRes) {
      if (userRes.data[0].myclub.indexOf(options.id) != -1) {
        that.setData({
          buttontext: "取消关注"
        })
      }
    })

    eventlist.where({
      host: options.id
    }).get().then(
      function(res) {
        var temp = res.data
        for (var i = 0; i < temp.length; i++) {
          temp[i].fmttimeto = util.formatTime(temp[i].timeto)
          temp[i].fmttimefrom = util.formatTime(temp[i].timefrom)
          if(date.getTime()>temp[i].timefrom.getTime()){
            temp[i].show=false
          }
          else{
            temp[i].show=true
          }
        }
        that.setData({
          events: temp
        })
      })
    clublist.where({
      _id: options.id
    }).get().then(
      function(res) {
        that.setData({
          clubinfo: res.data[0]
        })
      }
    )

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})